package top.jnaw.jee.utils;

import java.awt.image.BufferedImage;
import java.io.IOException;
import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;

/**
 * Created by lc on 17-6-1.
 */
public class Images {

  public static boolean crop(String source, String output, int size) {
    try {
      Thumbnails.of(source).size(size, size).outputFormat("jpg").toFile(output);
      return true;
    } catch (IOException e) {
      e.printStackTrace();
      return false;
    }
  }

  /**
   * 生成缩略图
   *
   * @param source 原文件
   */
  public static boolean thumb(String source, String output, int size) {
    boolean result = false;

    try {
      BufferedImage image = Thumbnails.of(source)
          .size(size, size).asBufferedImage();
      int width = image.getWidth();
      int height = image.getHeight();
      int ret = width < height ? width : height;

      Thumbnails.of(image)
          .sourceRegion(Positions.CENTER, ret, ret)
          .size(size, size)
          .outputQuality(0.25f)
          .outputFormat("jpg")
          .toFile(output);
      result = true;

    } catch (IOException e) {
      e.printStackTrace();
    }

    return result;
  }

}
